const hx = require('hbuilderx')

async function selectFile(title, description, label, filters) {
    const form = await hx.window.showFormDialog({
        formItems: [
            {
                type: 'fileSelectInput',
                name: 'path',
                mode: 'file',
                label: label,
                placeholder: '请选择文件路径',
                value: '',
                filters: filters
            }
        ],
        title: title,
        subtitle: description,
        width: 480,
        height: 180,
        submitButtonText: '确认',
        cancelButtonText: '取消',
        hideFooterSeparator: true,
        validate: function (form) {
            if (!form.path) {
                this.showError('请选择文件路径')
                return false
            }
            return true
        }
    })
    return form.path
}

async function selectPath(title, description, label) {
    const form = await hx.window.showFormDialog({
        formItems: [
            {
                type: 'fileSelectInput',
                name: 'path',
                mode: 'folder',
                label: label,
                placeholder: '请选择文件路径',
                value: ''
            }
        ],
        title: title,
        subtitle: description,
        width: 480,
        height: 180,
        submitButtonText: '确认',
        cancelButtonText: '取消',
        hideFooterSeparator: true,
        validate: function (form) {
            if (!form.path) {
                this.showError('请选择文件路径')
                return false
            }
            return true
        }
    })
    return form.path
}

async function createFile(title, description, label) {
    const form = await hx.window.showFormDialog({
        formItems: [
            {
                type: 'input',
                name: 'name',
                label: label,
                placeholder: '这是一个普通输入框',
                value: '',
                disabled: false
            }
        ],
        title: title,
        subtitle: description,
        width: 480,
        height: 180,
        submitButtonText: '确认',
        cancelButtonText: '取消',
        hideFooterSeparator: true,
        validate: function (form) {
            if (!form.name) {
                this.showError('请输入文件名')
                return false
            }
            return true
        }
    })
    return form.name
}

async function selectSyncFile(title, description) {
    const form = await hx.window.showFormDialog({
        formItems: [
            {
                type: 'fileSelectInput',
                name: 'path',
                mode: 'file',
                label: '本地文件',
                placeholder: '请选择同步目标',
                value: ''
            },
            {
                type: 'label',
                name: 'chunkSizeLabel',
                text: '<span style="color:green;">分割块大小(MB)</span>'
            },
            {
                type: 'comboBox',
                name: 'chunkSize',
                editable: true,
                items: ['1', '5', '10', '20', '50', '100'],
                text: '5'
            },
            {
                type: 'checkBox',
                name: 'isBackup',
                label: '建立备份',
                value: true
            }
        ],
        title: title,
        subtitle: description,
        width: 380,
        height: 180,
        submitButtonText: '开始同步',
        cancelButtonText: '取消',
        hideFooterSeparator: true,
        validate: function (form) {
            if (!form.path) {
                this.showError('请选择文件路径')
                return false
            }
            return true
        }
    })
    return form
}

function toUnitSize(size, decimals = 2) {
    if (size === 0) return '0 B'
    const dm = Math.max(0, decimals)
    const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
    const i = Math.floor(Math.log2(size) / 10)
    return parseFloat((size / Math.pow(1024, i)).toFixed(dm)) + ' ' + sizes[i]
}

module.exports = {
    selectSyncFile,
    selectFile,
    selectPath,
    createFile,
    toUnitSize
}
